//
//  Ext+NSAttributedStr.swift
//  langligelang
//
//  Created by 秦－政 on 2016/11/5.
//  Copyright © 2016年 pete. All rights reserved.
//

import UIKit

extension NSAttributedString {
    // 根据表情模型创建表情富文本
    class func attributedStringWithEmoticon(emoticon: SLEmoticon, font: UIFont) -> NSAttributedString {
    let image = UIImage(named: emoticon.fullPath!)
        // 根据 UIImage 对象创建文本附件(NSTextAttachment)
        let attachment = SLTextAttachment()
        // 给文本附件设置显示的属性
        attachment.image = image
        // 设置表情模型
        attachment.emoticon = emoticon
        // 设置字体大小
        let fontHeight = font.lineHeight
        //  文本附件bounds大小
        //  设置 bounds 会相应子控件的显示位置
        attachment.bounds = CGRect(x: 0, y: -4, width: fontHeight, height: fontHeight)
        // 根据文本附件创建富文本(NSAttributedString)
        let attributedStr = NSAttributedString(attachment: attachment)
        
        return attributedStr

    }
}
























